package cn.tedu.m3;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producter3 {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建连接工厂,并设置连接信息
        ConnectionFactory f1 = new ConnectionFactory();
        f1.setHost("192.168.64.140");
        f1.setPort(5672);
        f1.setUsername("admin");
        f1.setPassword("admin");
        Connection con = f1.newConnection(); //连接
        Channel c = con.createChannel(); //通信的通道
        //创建队列
        c.queueDeclare("helloworld-XQQ",true,false,false,null);

        //创建交换机
        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);
        //向交换机发送消息
        while (true){
            System.out.println("输入消息：");
            String s = new Scanner(System.in).nextLine();
            //第二个参数对于Fanout交换机无效，可写可不写
            c.basicPublish("logs", "", null, s.getBytes());
        }
    }
}
